package xyz.zhzby.cyzx.judge.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import xyz.zhzby.cyzx.judge.messenger.MessageSender;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zby
 * @date 2022/02/04
 * @Description 应用程序心跳. 用于向服务模块发送Keep-Alive信息.
 */
@Component
@Slf4j
public class ApplicationHeartbeat implements Runnable {

    @Value("${judger.username}")
    private String judgerUsername;

    @Value("${judger.description}")
    private String judgerDescription;

    @Autowired
    private MessageSender messageSender;

    @Override
    public void run() {
        Calendar calendar = Calendar.getInstance();
        Long currentTime = calendar.getTimeInMillis();

        Map<String, Object> mapMessage = new HashMap<>();
        mapMessage.put("event", "KeepAlive");
        mapMessage.put("username", judgerUsername);
        mapMessage.put("description", getDescription());
        mapMessage.put("heartbeatTime", currentTime);
        messageSender.sendMessage(mapMessage);
        log.info("心跳已发送至服务端");
    }

    /**
     * @Param [] []
     * @Return java.lang.String [评测机的描述信息]
     * @Author zby
     * @Date 2022/02/04
     * @Description 获取评测机的描述信息.
     */
    private String getDescription() {
        return judgerDescription;
    }

}
